package com.example.content_android.ui.me;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProviders;

import com.example.content_android.LoginActivity;
import com.example.content_android.MainActivity;
import com.example.content_android.R;
import com.example.content_android.me.MeHomeActivity;
import com.example.content_android.me.MessageMeActivity;
import com.example.content_android.me.RankingActivity;
import com.example.content_android.me.SettingActivity;
import com.example.content_android.ui.downloads.DownloadsFragment;
import com.example.content_android.ui.home.HomeFragment;

import static android.content.Context.MODE_PRIVATE;

public class MeFragment extends Fragment {
    AlertDialog.Builder builder;
    Button settingButton;
    Button rankingButton;
    Button messageMeButton;
    Button loginButton;
    Button logoutButton;


    private boolean isVisitor;

    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        View root = inflater.inflate(R.layout.fragment_me, container, false);

        SharedPreferences sp = getActivity().getSharedPreferences("userInfo", MODE_PRIVATE);
        isVisitor = sp.getBoolean("isVisitor", true);
        builder = new AlertDialog.Builder(root.getContext());
//        匿名内部类点击事件
        settingButton = root.findViewById(R.id.setting);
        settingButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isVisitor) {
                    //Creating dialog box
                    AlertDialog alert = builder.create();
                    alert.setMessage("请先登录系统！");
                    alert.show();
                }else{
                    Intent intent = new Intent(getActivity(), SettingActivity.class);
                    //获取当前activity的名称
                    intent.putExtra("comeFrom", getActivity().getClass().getSimpleName());
                    startActivity(intent);
                }


            }
        });


        rankingButton = root.findViewById(R.id.ranking);
        rankingButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isVisitor) {
                    AlertDialog alert = builder.create();
                    alert.setMessage("请先登录系统！");
                    alert.show();
                }else{
                    Intent intent = new Intent(getActivity(), RankingActivity.class);
                    //获取当前activity的名称
                    intent.putExtra("comeFrom", getActivity().getClass().getSimpleName());
                    startActivity(intent);
                }

//                builder.setMessage(R.string.login_alert) .setTitle(R.string.title_ranking);
//                AlertDialog alert = builder.create();
////                alert.setMessage("请先登录系统！");
//                alert.show();
            }
        });


        messageMeButton = root.findViewById(R.id.messageMe);
        messageMeButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isVisitor) {
                    AlertDialog alert = builder.create();
                    alert.setMessage("请先登录系统！");
                    alert.show();
                }else{
                    Intent intent = new Intent(getActivity(), MessageMeActivity.class);
                    //获取当前activity的名称
                    intent.putExtra("comeFrom", getActivity().getClass().getSimpleName());
                    startActivity(intent);
                }

//                builder.setTitle(R.string.title_messageMe);
////                //Setting message manually and performing action on button click
//                builder.setMessage("Do you want to close this application ?")
//                        .setCancelable(true)
//                        .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
//                            public void onClick(DialogInterface dialog, int id) {
//                                //finish（）会关闭应用
////                                finish();
//                                dialog.cancel();
//                                Toast.makeText(getApplicationContext(), "you choose yes action for alertbox", Toast.LENGTH_SHORT).show();
//                            }
//                        })
//                        .setNegativeButton("No", new DialogInterface.OnClickListener() {
//                            public void onClick(DialogInterface dialog, int id) {
//                                //  Action for 'NO' Button
//                                dialog.cancel();
//                                Toast.makeText(getApplicationContext(), "you choose no action for alertbox", Toast.LENGTH_SHORT).show();
//                            }
//                        });
//                //Creating dialog box
//                AlertDialog alert = builder.create();
//
//                alert.show();
            }
        });

// 获取控件id
        loginButton = root.findViewById(R.id.login);

        // 监听点击事件
        loginButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                从一个Activity的Fragment跳转到另外一个Activity
                Intent intent = new Intent(getActivity(), LoginActivity.class);
                //获取当前activity的名称
                intent.putExtra("comeFrom", getActivity().getClass().getSimpleName());
                startActivity(intent);
            }
        });

        logoutButton = root.findViewById(R.id.logout);

        if (!isVisitor) {
            //View.GONE 不可见不占位  View.VISIBLE 可见   View.INVISIBLE 不可见
//            loginButton.setVisibility(View.INVISIBLE);
            loginButton.setVisibility(View.GONE);
            logoutButton.setVisibility(View.VISIBLE);

        }

        // 监听点击事件
        logoutButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                AlertDialog alert = builder.create();
//                alert.setIcon(R.drawable.icon_launch);//设置对话框的图标
                alert.setTitle("是否确定退出？");//设置对话框的标题
//                alert.setMessage("显示带取消、中立和确定按钮的对话框！");//设置对话框显示的内容
                //添加“取消”按钮
                alert.setButton(DialogInterface.BUTTON_NEGATIVE, "取消",
                        new DialogInterface.OnClickListener() {


                            @Override
                            public void onClick(DialogInterface dialog, int which) {
//                                Toast.makeText(getActivity(), "您单击了取消按钮", Toast.LENGTH_SHORT).show();
                            }
                        });
                //添加“确定”按钮
                alert.setButton(DialogInterface.BUTTON_POSITIVE, "确定",
                        new DialogInterface.OnClickListener() {


                            @Override
                            public void onClick(DialogInterface dialog, int which) {
//                                Toast.makeText(getActivity(), "您单击了确定按钮", Toast.LENGTH_SHORT).show();
                                //                从一个Activity的Fragment跳转到另外一个Activity
                                Intent intent = new Intent(getActivity(), MainActivity.class);
                                //获取当前activity的名称
                                intent.putExtra("comeFrom", getActivity().getClass().getSimpleName());

                                SharedPreferences sp = getActivity().getSharedPreferences("userInfo", MODE_PRIVATE);
                                if (sp.contains("isVisitor")) {
                                    isVisitor = sp.getBoolean("isVisitor", true);
                                    if (!isVisitor) {
                                        SharedPreferences.Editor editor = sp.edit();
                                        editor.putBoolean("isVisitor", true);
                                        editor.commit();
                                    }
                                }
                                loginButton.setVisibility(View.VISIBLE);
                                logoutButton.setVisibility(View.GONE);
                                startActivity(intent);
                            }
                        });

                alert.show();//显示对话框


            }
        });

        return root;
    }
}